package com.athuiju.fenxiaoback.controller;


import com.athuiju.fenxiaoback.entity.TbSpu;
import com.athuiju.fenxiaoback.entity.UmsMemberReceiveAddress;
import com.athuiju.fenxiaoback.entity.vo.UmsMemberReceiveAddressVo;
import com.athuiju.fenxiaoback.service.UmsMemberReceiveAddressService;
import com.athuiju.fenxiaoback.utils.JwtUtils;
import com.athuiju.fenxiaoback.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 会员收货地址 前端控制器
 * </p>
 *
 * @author athuiju
 * @since 2021-07-06
 */
@Api(description="地址",tags = "地址")
//@CrossOrigin //11
@RestController
@RequestMapping("/fenxiaoback/ums-member-receive-address")
public class UmsMemberReceiveAddressController {

    @Autowired
    UmsMemberReceiveAddressService addressService;

    @ApiOperation(value = "根据id----查询地址")
    @GetMapping("getById/{addredid}")
    public R getById(@PathVariable("addredid") Long addresid,HttpServletRequest request) {

        UmsMemberReceiveAddress address = addressService.getById(addresid);

        return R.ok().put("data", address);
    }



    @ApiOperation(value = "查询用户地址列表")
    @GetMapping("listAddress")
    public R listAddress(HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        QueryWrapper<UmsMemberReceiveAddress> addWrapper=new QueryWrapper();
        addWrapper.eq("member_id",memberId);
        addWrapper.orderByDesc("default_status");
        addWrapper.orderByDesc("gmt_modified");

        List<UmsMemberReceiveAddress> list = addressService.list(addWrapper);

        return R.ok().put("data", list);
    }


    @ApiOperation(value = "删除用户地址列表")
    @PostMapping("delAddress")
    public R delAddress(@RequestBody List<Long> ids, HttpServletRequest request) {
           boolean b = addressService.removeByIds(ids);

        return b ? R.ok().put("msg", "删除成功") : R.error().put("msg","删除失败");
    }


    @ApiOperation(value = "添加用户地址")
    @PostMapping("addAddress")
    public R addAddress(@RequestBody UmsMemberReceiveAddressVo address,HttpServletRequest request) {
        //UmsMemberReceiveAddressVo
        //UmsMemberReceiveAddress

        UmsMemberReceiveAddress ura=new UmsMemberReceiveAddress();
        BeanUtils.copyProperties(address,ura);


        String memberid = JwtUtils.getMemberIdByJwtToken(request);

        ura.setMemberId(Long.parseLong(memberid));

        addressService.save(ura);

        return  R.ok().put("msg", "添加成功").put("addressId",ura.getId());
    }

    @ApiOperation(value = "修改用户地址")
    @PostMapping("updateAddress")
    public R updateAddress(@RequestBody UmsMemberReceiveAddress address,HttpServletRequest request) {
        String memberid = JwtUtils.getMemberIdByJwtToken(request);

        address.setMemberId(Long.parseLong(memberid));

        addressService.updateById(address);

        return  R.ok().put("msg", "修改成功");
    }



    @ApiOperation(value = "修改用户默认地址")
    @GetMapping("updateDefault/{addressId}")
    public R updateDefault(@PathVariable("addressId") Long addressId, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        //1.判断地址是否存在
        QueryWrapper<UmsMemberReceiveAddress> addWrapper1=new QueryWrapper();
        addWrapper1.eq("member_id",memberId);
        addWrapper1.eq("id",addressId);
        UmsMemberReceiveAddress address = addressService.getOne(addWrapper1);
        if (address==null){
            return R.error().put("msg","地址不存在");
        }

        //2.查询这个用户的所有地址
        QueryWrapper<UmsMemberReceiveAddress> addWrapper=new QueryWrapper();
        addWrapper.eq("member_id",memberId);
        addWrapper.orderByDesc("gmt_modified");
        List<UmsMemberReceiveAddress> list = addressService.list(addWrapper);


        for (int i=0;i<list.size();i++){
           long addid= list.get(i).getId();

            if (addressId==addid){
                addressService.updateDefault(list.get(i).getId(),true);
            }else{
                addressService.updateDefault(list.get(i).getId(),false);
            }
        }
        return R.ok();
    }


}

